/**
 * 简单工厂
 */

// 1. 创建不同类，工厂对象根据输入实例化不同的类
function Dog(name) {
  this.name = name;
  this.run = function () {
    console.log("run!");
  };
}

function Fish(name) {
  this.name = name;
  this.swim = function () {
    console.log("swim!");
  };
}

function factory(type, name) {
  switch (type) {
    case "dog":
      return new Dog(name);
    case "fish":
      return new Fish(name);
    default:
      return null;
  }
}

let dog = new factory("dog", "husky");
dog.run();
let fish = new factory("fish", "salmon");
fish.swim();

// 2. 创建一个对象，根据不同的输入包装这个对象
function factory2(type, name) {
  let o = {};
  switch (type) {
    case "dog":
      o.name = name;
      o.run = function () {
        console.log("run2!");
      };
      return o;
    case "fish":
      o.name = name;
      o.swim = function () {
        console.log("swim2!");
      };
      return o;
    default:
      return o;
  }
}

let dog2 = new factory2("dog", "husky");
dog2.run();
let fish2 = new factory2("fish", "salmon");
fish2.swim();
